<?php
/**
 * register service for console application
 * @var $app \micro\console\App
 */

require __DIR__ . '/common-services.php';

//
// can register custom service.
//

$app->set('onNotFound', function(\micro\console\App $app)
{
    $cmd = $app->input->getCommand();

    $app->logger->warning("Command not found: $cmd");

    $msg = " <error>Do you want to run the command [$cmd] does not exist!</error> \n\n";

    if ( APP_DEBUG ) {
        $msg .= ". <info>Maybe you not config command route [$cmd] in router file.</info>\n";
    }

    $app->output->write($msg);
});

$app->set('cacheDb', function() {
    $path = PROJECT_PATH . '/app/data/caches.db3';
    $pdo = new \PDO("sqlite:$path");

    // return new Memcache();
    return $pdo;
});

// can also use property add service.
$app->on('beforeRun', function(\micro\console\App $app)
{
    $cmd = $app->input->getCommand();

    $app->logger->debug("Console App start run. CMD: $cmd. Time: " . microtime(true));
});

$app->on('afterRun', function(\micro\console\App $app)
{
    $app->logger->debug('Console App end run. Time: ' . microtime(true));
});

$app->on('appStop', function(\micro\console\App $app)
{
    $app->logger->debug('Console App stopped. Time: ' . microtime(true));

    \inhere\library\utils\LiteLogger::flushAll();
});
